package com.hckjsec.law.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hckjsec.common.core.domain.entity.SysDictData;
import com.hckjsec.common.utils.*;
import com.hckjsec.system.service.ISysDictTypeService;
import com.hckjsec.system.service.ISysPostService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.law.domain.WebLawReserve;
import com.hckjsec.law.service.IWebLawReserveService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公共法律服务预约Controller
 * 
 * @author cyt
 * @date 2021-11-12
 */
@RestController
@RequestMapping("/system/lawreserve")
public class WebLawReserveController extends BaseController
{
    @Autowired
    private IWebLawReserveService webLawReserveService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysPostService postService;

    /**
     * 查询公共法律服务预约列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebLawReserve webLawReserve)
    {
        startPage();
        if(!SecurityUtils.isAdminPost(SecurityUtils.getUserId(),postService.selectPostListByUserId(SecurityUtils.getUserId()))){
            webLawReserve.setDeptId(SecurityUtils.getDeptId());
            webLawReserve.getParams().put("personal", SecurityUtils.getUserId());
        }
        List<WebLawReserve> list = webLawReserveService.selectWebLawReserveList(webLawReserve);
        return getDataTable(list);
    }

    /**
     * 导出公共法律服务预约列表
     */
    @Log(title = "公共法律服务预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebLawReserve webLawReserve)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webLawReserve.setDeptId(SecurityUtils.getDeptId());
            webLawReserve.getParams().put("personal", SecurityUtils.getUserId());
        }
        List<WebLawReserve> list = webLawReserveService.selectWebLawReserveList(webLawReserve);
        ExcelUtil<WebLawReserve> util = new ExcelUtil<WebLawReserve>(WebLawReserve.class);
        return util.exportExcel(list, "reserve");
    }

    /**
     * 获取公共法律服务预约详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Map<String,String> zjlxryMap = new HashMap<String,String>();
        List<SysDictData> zjlxryList=dictTypeService.selectDictDataByType(ComUtil.CONFIG_PERSONNELTYPE);
        zjlxryList.forEach(clKv ->{
            zjlxryMap.put(clKv.getDictValue(),clKv.getDictLabel());
        });
        WebLawReserve webLawReserve = webLawReserveService.selectWebLawReserveById(id);
        webLawReserve.setCertificatestype(zjlxryMap.get(webLawReserve.getCertificatestype()));
        return AjaxResult.success(webLawReserve);
    }

    /**
     * 新增公共法律服务咨询
     */
    @Log(title = "公共法律服务预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebLawReserve webLawReserve)
    {
        return toAjax(webLawReserveService.insertWebLawReserve(webLawReserve));
    }

    /**
     * 修改公共法律服务咨询
     */
    @Log(title = "公共法律服务预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebLawReserve webLawReserve)
    {
        if (StringUtils.isNull(webLawReserve.getScheduletime())){
            webLawReserve.setScheduletime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, webLawReserve.getReserveTime()));
        }
        List<String> phonelist = new ArrayList<String>();
        webLawReserve.setCertificatestype(null);
        webLawReserve.setReviewer(SecurityUtils.getUserId());
        webLawReserve.setReviewerName(SecurityUtils.getNickName());
        webLawReserve.setReviewerdatetime(DateUtils.getNowDate());
        int rows = webLawReserveService.updateWebLawReserve(webLawReserve);
        if (rows >0){
            phonelist.add("+86"+webLawReserve.getPhone());
            TxSmsUtil.smsData(ComUtil.LAW_SECRETID,ComUtil.LAW_SECRETKEY,ComUtil.LAW_SMSSDKAPPID,ComUtil.LAW_SIGN,phonelist,"15","","","");
        }
        return toAjax(rows);
    }

    /**
     * 删除公共法律服务咨询
     */
    @Log(title = "公共法律服务预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webLawReserveService.deleteWebLawReserveByIds(ids));
    }
}
